<?php
include_once "dao/Distributor.php";
include_once "dao/DistributorDAO.php";

$func = $_GET['func'];


if($func==("search")) {
	$distID = $_GET['distID'];
	$name = $_GET['name'];
	searchDistributor($distID, $name);
}
else if($func==("collect")) {
	$distID = $_GET['distID'];
	$distributorDAO = DistributorDAO::getInstance();
	$distributorDAO->setCollected($distID);
	
	header( 'Location: index.html' );
 	exit();
	
}
else if($func==("uncollect")) {
	$distID = $_GET['distID'];
	$distributorDAO = DistributorDAO::getInstance();
	$distributorDAO->setUncollected($distID);	
	
	header( 'Location: index.html' );
 	exit();
}
else if($func==("viewDetails")) {
	$distID = $_GET['distID'];
	$distributorDAO = DistributorDAO::getInstance();
	
	$result = $distributorDAO->searchDistributorByID($distID);
	print_r($result);
}

function searchDistributor($distID, $name) {
	
	if($distID != "") {
		$distributorDAO = DistributorDAO::getInstance(); 
		$result = $distributorDAO->searchDistributorByID($distID);
		
		displayResult($result);
	}
	else if($name != null){
		$distributorDAO = DistributorDAO::getInstance();
		$result = $distributorDAO->searchDistributorByName($name);
		
		displayResult($result);
	}
	else {
		echo "<h3> Opps, You did not enter anything.</h3>";
	}
	
}

function displayResult($result) {
	
	echo "<h3>".count($result)." Records Found</h3>";
	
	echo "<table cellpadding='0' cellspacing='0'>";
	echo "<thead>";
	echo "<tr>";
	echo "<th>Package No</th>";
	echo "<th>Distributor ID</th>";
	echo "<th>Name</th>";
	echo "<th>Assoc 1</th>";
	echo "<th>Assoc 2</th>";
	echo "<th>Assoc 3</th>";
	echo "<th>Pin Title</th>";
	echo "<th>Pax</th>";
	echo "<th>Collection Status</th>";
	echo "</tr>";
	echo "</thead>";
	echo "<tbody>";
	
	
	foreach($result as $dist) {
		echo "<tr>";
		echo "<td>".$dist->getPackNo()."</td>";
		echo "<td>".$dist->getDistID()."</td>";
		echo "<td>".$dist->getName()."</td>";
		echo "<td>".$dist->getAssoc1()."</td>";
		echo "<td>".$dist->getAssoc2()."</td>";
		echo "<td>".$dist->getAssoc3()."</td>";
		echo "<td>".$dist->getPinTitle()."</td>";
		echo "<td>".$dist->getPax()."</td>";
		
		$distID = $dist->getDistID();
		
		if($dist->getAttendance() == "N")
			echo "<td><a href='searchDistributor.php?func=collect&distID=$distID'>Uncollect</a></td>";
		else 
			echo "<td><a href='searchDistributor.php?func=uncollect&distID=$distID'>Collected</a></td>";
		echo "</tr>";
	}
	
	
	echo "</tbody>";
	echo "</table>";
	
	
	
	
	
}
			
			


?>

